package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.reports.forms.ProgramReleaseYieldReportForm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Sep 21, 2005
 * Time: 1:50:46 PM
 * To change this template use Options | File Templates.
 */
public class ProgramReleaseYieldReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/progamReleaseYieldReport.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {
        HttpSession session = request.getSession();
        ProgramReleaseYieldReportForm programReleaseYieldReportForm = (ProgramReleaseYieldReportForm)form;
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );
        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }
        HashMap errors = new HashMap();

        ArrayList baseProductsList   = null;
        ArrayList productsList = null;
        ArrayList vendorsList    = null;
        int baseProductId =  programReleaseYieldReportForm.getBaseProductId();

        try
        {
            baseProductsList = ProductDao.loadChipBaseProducts( null );
            if( baseProductId == 0 )
            {
                productsList = RelaseDAO.getPartNumbers( Constants.LOCATION_TYPE_CLASS_STORE );
            }
            else
            {
                productsList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductId, Constants.LOCATION_TYPE_CLASS_STORE);
            }
            vendorsList = RelaseDAO.loadProcessVendors( Constants.LOCATION_TYPE_CLASS_STORE );
        }
        catch( Exception e )
        {
            e.printStackTrace();
            errors.put(ERROR_INTERNAL_SERVER_ERROR, null);
        }
        request.setAttribute( "productList", productsList );
        request.setAttribute( "baseProductList", baseProductsList );
        request.setAttribute( "vendorList", vendorsList );
        return mapping.findForward( "success" );
    }
 }

